package com.pdd.controller;


import com.pdd.beans.Order_detail;
import com.pdd.beans.User;
import com.pdd.dto.DTO;
import com.pdd.service.Order_detailService;
import com.pdd.vo.Order_detail.Order_detailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单详情控制器
 */
@Api(description = "订单详情接口",tags = {"订单详情接口"})
@CrossOrigin(origins = { "*" })
@Controller
public class Order_detailController {

    @Autowired
    @Setter
    private Order_detailService order_detailService;

    @ApiOperation(value = "添加订单详情信息")
    @RequestMapping(value = "/order_detail", method = RequestMethod.POST)
    @ResponseBody
    public DTO addOrder_detail(@RequestBody Order_detailVO vo){
        // 1. 封装参数
        Order_detail order_detail = new Order_detail();
        order_detail.setO_id(vo.getO_id());
        order_detail.setP_id(vo.getP_id());
        order_detail.setOd_count(vo.getOd_count());

        // 2. 执行新增
        boolean result = order_detailService.addOrder_detail(order_detail);

        // 3. 创建DTO，并返回
        if (result){
            return DTO.success();
        }else{
            return DTO.error();
        }
    }
    @ApiOperation(value = "删除订单详情")
    @RequestMapping(value = "/order_detail/{od_id}", method = RequestMethod.DELETE)
    public DTO delOrder_detail(@PathVariable(value = "od_id") Integer od_id){

        boolean result = order_detailService.delOrder_detail(od_id);
        if (result){
            return DTO.success();
        }else {
            return DTO.error();
        }
    }

    @ApiOperation(value = "更新订单详情信息")
    @RequestMapping(value = "/order_detail", method = RequestMethod.PUT)
    @ResponseBody
    public DTO uplOrder_detail(Order_detailVO vo){
        // 1. 封装参数
        Order_detail order_detail = new Order_detail();
        order_detail.setO_id(vo.getO_id());
        order_detail.setP_id(vo.getP_id());
        order_detail.setOd_count(vo.getOd_count());

        // 2. 执行更新
        boolean result = order_detail.equals(order_detail);

        // 3. 创建DTO，并返回
        if (result){
            return DTO.success();
        }else{
            return DTO.error();
        }
    }

    @ApiOperation(value = "查询订单详情信息")
    @RequestMapping(value = "/order_detail", method = RequestMethod.GET)
    @ResponseBody
    public DTO getOrder_detail(){
        // 1. 执行查询操作
        List<Order_detail> list = order_detailService.getOrder_detail();

        // 2. 封装 DTO
        DTO dto = new DTO(list);

        // 3. 返回结果
        return dto;
    }
    @ApiOperation(value = "根据订单ID查询订单详情信息")
    @RequestMapping(value = "/order_detail/{o_id}", method = RequestMethod.GET)
    @ResponseBody
    public DTO getById(@PathVariable(value = "o_id") Integer o_id){
        // 1. 执行查询操作
        Order_detail list = order_detailService.getById(o_id);

        // 2. 封装 DTO
        DTO dto = new DTO(list);

        // 3. 返回结果
        return dto;
    }
}

